import { Controller } from 'egg';

class TopicController extends Controller {

    /**
     * 查询话题列表
     */
    public async searchTopicList() {
        const { ctx } = this;
        const result = await ctx.service.topicService.searchTopicList();
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 新增话题
     */
    public async createTopic() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                topic_title: 'string',
                topic_picture: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.topicService.createTopic(request.body);
        ctx.body = ctx.helper.responseSuccess('新增成功');
    }

    /**
     * 修改话题
     */
    public async updateTopic() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                topic_id: 'string',
                topic_title: 'string',
                topic_picture: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.topicService.updateTopic(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }

    /**
     * 启用、停用话题
     */
    public async startAndStopTopic() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                topic_id: 'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.topicService.startAndStopTopic(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }
}

export default TopicController;